package com.nowcoder.community.controller;

import com.nowcoder.community.service.DataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;

/**
 * @Author: shuhuang
 * @description:
 * @Date: 2023/1/30 22:29
 * @Version java version 1.8
 */
@Controller
public class DataController {
    @Autowired
    private DataService dataService;

    /**
     * 统计页面
     */
    @RequestMapping(path = "/data")
    public String getDataPage() {
        return "/site/admin/data";
    }

    //统计网站UV
    @PostMapping(path = "/data/uv")
    public String getUv(@DateTimeFormat(pattern = "yyyy-MM-dd") Date start,
                        @DateTimeFormat(pattern = "yyyy-MM-dd") Date end, Model model) {
        long result = dataService.calculateUv(start, end);
        model.addAttribute("uvResult", result);
        model.addAttribute("uvStart",start);
        model.addAttribute("uvEnd", end);
        return "forward:/data";
    }

    //统计网站DAU
    @PostMapping(path = "/data/dau")
    public String getDau(@DateTimeFormat(pattern = "yyyy-MM-dd") Date start,
                         @DateTimeFormat(pattern = "yyyy-MM-dd") Date end, Model model) {
        long result = dataService.calculateDau(start, end);
        model.addAttribute("dauResult", result);
        model.addAttribute("dauStart", start);
        model.addAttribute("dauEnd", end);
        return "forward:/data";
    }
}
